﻿using FriendStar.Code;
using FriendStar.Domain.Entity.VBaseData;
using FriendStar.Domain.IRepository.VBaseData;
using FriendStar.Repository.VBaseData;
using System.Collections.Generic;
using System.Linq;
using SqlSugarRepository;
using FriendStar.Domain.Entity.BaseData;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System;

namespace FriendStar.Application.VBaseData
{
    public class V_BUS_SJ_DictionaryApp
    {
        private IV_BUS_SJ_DictionaryRepository service = new V_BUS_SJ_DictionaryRepository();
        private string useUnit = Configs.GetValue("UseUnit");
        public List<V_BUS_SJ_DictionaryEntity> GetList()
        {
            return service.IQueryable().ToList();
        }

        public List<V_BUS_SJ_DictionaryEntity> GetSJ_DictionaryEntity(string useUnit, string fClassNO)
        {
            var expression = ExtLinq.True<V_BUS_SJ_DictionaryEntity>();
            if (!string.IsNullOrEmpty(useUnit))
            {
                expression = expression.And(t => t.UseUnit == useUnit);
            }
            if (!string.IsNullOrEmpty(fClassNO))
            {
                expression = expression.And(t => t.FClassNO == fClassNO);
            }
            return service.IQueryable(expression).Select(p => new V_BUS_SJ_DictionaryEntity { FTypeNO = p.FTypeNO, FTypeName = p.FTypeName,FTypeNOName=p.FTypeNOName }).OrderBy(p => p.SortCode).ToList();
        }

        public List<V_BUS_SJ_DictionaryEntity> GetSJ_DictionaryEntityByTypeNo(string useUnit, string typeNo)
        {
            var expression = ExtLinq.True<V_BUS_SJ_DictionaryEntity>();
            if (!string.IsNullOrEmpty(useUnit))
            {
                expression = expression.And(t => t.UseUnit == useUnit);
            }
            if (!string.IsNullOrEmpty(typeNo))
            {
                expression = expression.And(t => t.FTypeNO == typeNo);
            }
            return service.IQueryable(expression).Select(p => new V_BUS_SJ_DictionaryEntity { FTypeNO = p.FTypeNO, FTypeName = p.FTypeName, FTypeNOName = p.FTypeNOName }).OrderBy(p => p.SortCode).ToList();
        }

        public List<V_BUS_SJ_DictionaryEntity> GetStoreType()
        {
            //29为仓库类型标识类型
            return GetSJ_DictionaryEntity(useUnit, "29");
        }
        public List<BUS_SJ_DictionaryEntity> GetWayNodesForNotice(string useUnit)
        {
            var expression = ExtLinq.True<V_BUS_SJ_DictionaryEntity>();
            if (!string.IsNullOrEmpty(useUnit))
            {
                expression = expression.And(t => t.UseUnit == useUnit);
            }
            expression = expression.And(t => t.FTypeNO=="CR"|| t.FTypeNO == "CW");
            return service.IQueryable(expression).Select(p => new BUS_SJ_DictionaryEntity { FTypeNO = p.FTypeNO, FTypeName = p.FTypeName }).OrderBy(p => p.SortCode).ToList();
        }


        public List<Dictionary<string, object>> GetOccurrenceUnit()
        {
            List<Dictionary<string, object>> result = null;

            Dictionary<string, object> item = null;

            string sql = @"select UnitNO,UnitShortName  from  ZSJ_B1_GH_SupplierInformation  where  (IsProduct='1'  or IsCustomer='1')";

            result = new List<Dictionary<string, object>>();

            using (SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.GetConnection(), CommandType.Text, sql))
            {
                while (reader.Read())
                {
                    item = new Dictionary<string, object>();

                    for (int i = 0; i < reader.FieldCount; i++)
                    {
                        item.Add(reader.GetName(i), reader[i]);
                    }

                    result.Add(item);
                }
            }

            return result;
        }

        public List<Dictionary<string,object>> GetDefaultDate(string useUnit,string FTypeNum)
        {
            List<Dictionary<string, object>> result = null;

            Dictionary<string, object> item = null;

            string sql = @"select top 1 FItemParam  from Sys_RunParam  where  UseUnit =@useUnit and FTypeNum =@FTypeNum";

            result = new List<Dictionary<string, object>>();
            //K_8_LJRKH

            SqlParameter[] pars = new SqlParameter[] {
                new SqlParameter("@useUnit",useUnit),
                new SqlParameter("@FTypeNum",FTypeNum)
            };


            using (SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.GetConnection(), CommandType.Text, sql, pars))
            {
                while (reader.Read())
                {
                    item = new Dictionary<string, object>();

                    for (int i = 0; i < reader.FieldCount; i++)
                    {
                        item.Add(reader.GetName(i), reader[i]);
                    }

                    result.Add(item);
                }
            }

            return result;
        }

        public int SetDefaultDate(string useUnit, string start, string end,string user,string type)
        {
            int result = 0;

            Dictionary<string, string> procs = new Dictionary<string, string>();


            string sql = string.Format(@"EXEC {0} @useUnit,@start,@end,@user",type);

            
            try
            {
                SqlParameter[] pars = new SqlParameter[] {
                new SqlParameter("@useUnit",useUnit),
                new SqlParameter("@start",start),
                new SqlParameter("@end",end),
                new SqlParameter("@user",user)
            };

                result = SqlHelper.ExecuteNonQuery(SqlHelper.GetConnection(), CommandType.Text, sql, pars);
            }
            catch 
            {

                result = -1;
            }

            return result;

        }
    }
}
